<?php
/**
 * Description of productivity
 * 
 * @author trongkhoi
 * @since Feb 12, 2014 - 2:39:19 PM
 */
$css = "#productivity-table .box-content{min-height:293px}";
cs()->registerCss("productivity", $css);
?>
<div class="row-fluid" id="productivity-table">
    <div class="box span12">
        <div class="box-header">
            <h2>
                <i class="icon-align-justify"></i>Productivity
            </h2>
        </div>
        <div class="box-content">
            <div class="schedules-filter">
                <form>                    
                    <?php if ($this->isAdmin): ?>                        
                        <?php
                        echo CHtml::dropDownList("biz", $this->selectBizUkey, Users::getAllBussiness(TRUE), array(
                            "onchange" => "selectBizProductivity($(this))",
                            "data-rel" => "chosen"
                        ));
                        ?>
                    <?php endif; ?>
                </form>
            </div>
            <?php $staffs = Users::getAllStaffByBusiness($this->selectBizUkey, TRUE); ?>
            <?php if ($staffs): ?>
                <div class="table-responsive">
                    <table class="table table-bordered table-condensed schedules-table">
                        <thead>
                            <tr>
                                <th class="center">Monthly/Staff</th>
                                <?php foreach ($staffs as $staff_name): ?>                            
                                    <th class="center"><?php echo $staff_name; ?></th>
                                <?php endforeach; ?>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    Total roster hours
                                </td>
                                <?php foreach ($staffs as $ukey => $staff_name): ?>
                                    <td class="center">
                                        <?php echo SchedulesUser::TotalRosterHour($ukey) ?>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                            <tr>
                                <td>
                                    Total clocked hours
                                </td>
                                <?php foreach ($staffs as $ukey => $staff_name): ?>
                                    <td class="center">
                                        <?php echo SchedulesUser::TotalRosterHour($ukey) ?>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                            <tr>
                                <td>
                                    Total service hours
                                </td>
                                <?php foreach ($staffs as $ukey => $staff_name): ?>
                                    <td class="center">
                                        <?php echo round(SchedulesUser::totalServiceHourOfStaff($ukey), 2) ?>
                                    </td>
                                <?php endforeach; ?>
                            </tr>

                            <tr>
                                <td>
                                    Total open hours
                                </td>

                                <?php foreach ($staffs as $ukey => $staff_name): ?>

                                    <td class="center">
                                        <?php echo round(SchedulesUser::TotalRosterHour($ukey) - SchedulesUser::totalServiceHourOfStaff($ukey), 2) ?>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                            <tr>
                                <td>
                                    % of productivity
                                </td>
                                <?php foreach ($staffs as $ukey => $staff_name): ?>
                                    <td class="center">
                                        <?php
                                        $total_roster = SchedulesUser::TotalRosterHour($ukey);
                                        if (FALSE === $total_roster OR $total_roster == 0)
                                            echo 0.00;
                                        else
                                        {
                                            $percent = (SchedulesUser::totalServiceHourOfStaff($ukey) / $total_roster) * 100;
                                            echo round($percent, 2);
                                        }
                                        ?>%
                                    </td>
                                <?php endforeach; ?>
                            </tr>

                            <tr>
                                <td>
                                    Total revenue
                                </td>
                                <?php foreach ($staffs as $ukey => $staff_name): ?>
                                    <td class="center">
                                        $<?php
                                        $total = Receipt::revenueStaffThisMonth($ukey);
                                        echo number_format($total, 2);
                                        ?>
                                    </td>
                                <?php endforeach; ?>
                            </tr>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<script>
    $("[data-rel='chosen']").chosen();
    function selectBizProductivity(el)
    {
        $.ajax({
            url: '<?php echo Yii::app()->createUrl('site/_reloadProductivity') ?>',
            type: 'POST',
            data: {
                biz_ukey: el.val()
            },
            success: function(response) {
                $('#productivity-table').remove();
                $("hr#productivity").before(response);
            }
        });
    }
</script>